package com.smh.mianshi.initializer;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.HashMap;

/**
 * @author shiminghui
 * @date 2024/9/26 20:39
 * @description: TODO
 */
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
    //ioc 容器创建完成后执行,常用来进行环境变量的注入
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        //给上下文context对象注入环境
        HashMap<String, Object> myMap = new HashMap<>();
        myMap.put("applicationName", "qwe");

        ConfigurableEnvironment environment = applicationContext.getEnvironment();//获取环境
        MutablePropertySources propertySources = environment.getPropertySources();//获取属性源
        propertySources.addLast(new MapPropertySource("myMap", myMap));
//        throw new RuntimeException();
    }
}
